import numpy as np
import matplotlib.pyplot as plt

class cal_temperature:
    def __init__(self, diff_step, R_1, R_2, C_in, C_wall,P_N=8000):
        self.dt = diff_step
        self.R_1 = R_1
        self.R_2 = R_2
        self.C_in = C_in
        self.C_wall = C_wall
        self.P_N = P_N

        self.A = np.array([-1 / (R_1 * C_in), 1 / (R_1 * C_in), 1 /
                     (R_1 * C_wall), (-1 / (R_1 * C_wall) - 1 / (R_2* C_wall))]).reshape((2,2))
    def set_start_theta(self,theta_in,theta_wall):
        self.theta = np.array([theta_in, theta_wall]).reshape((2,1))
    
    def reset_B_P(self):
        self.P_status = 1
        self.B = np.array([self.P_N / self.C_in, 0]).reshape((2,1))
    def reset_B_0(self):
        self.P_status = 0
        self.B = np.array([0, 0]).reshape((2,1))
    def get_theta(self):
        return self.theta
    def reset_out_tempurature(self,theta_out):
        self.bias = np.array([0, theta_out / (self.R_2 * self.C_wall)]).reshape((2,1))

    
    def rk45(self):
        k_1 = np.dot(self.A , self.theta) + self.B +self.bias
        k_2 = np.dot(self.A ,(self.theta + self.dt / 2 * k_1)) +self.B +self.bias
        k_3 = np.dot(self.A , (self.theta + self.dt / 2 * k_2)) + self.B +self.bias
        k_4 = np.dot(self.A ,(self.theta + self.dt * k_3)) + self.B +self.bias
        self.theta = self.theta + np.dot(self.dt ,(k_1 + 2 * k_2 + 2 * k_3 + k_4) / 6)
